package com.zzt.book.service;

import com.zzt.book.enums.BookStatusEnum;
import com.zzt.book.mapper.BookMapper;
import com.zzt.book.model.BookInfo;
import com.zzt.book.model.PageRequest;
import com.zzt.book.model.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-02-21
 * Time: 16:22
 */
@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public void addBook(BookInfo bookInfo) {
        bookMapper.addBook(bookInfo);
    }

    public ResponseResult<BookInfo> getListByPage(PageRequest pageRequest){
        //1.获得总图书数
        //2.获得当前页的数据
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos = bookMapper.selectBookByPage(pageRequest);
        for(BookInfo bookInfo: bookInfos){
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
//            if(bookInfo.getStatus() == 1){
//                bookInfo.setStatusCN("可借阅");
//            }else{
//                bookInfo.setStatusCN("不可借阅");
//            }
        }
//        ResponseResult<BookInfo> responseResult = new ResponseResult<>();
//        responseResult.setTotal(count);
//        responseResult.setRecords(bookInfos);
        return new ResponseResult<BookInfo>(count, bookInfos, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public void updateBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids){
        return bookMapper.batchDelete(ids);
    }
}
